package com.atguigu.flink.streamapi.transform;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

/**
 * Created by Smexy on 2022/11/21
 *
 *  flatMap: 最强大的，可以替换 map 和 filter
 */
public class Demo4_FlatMap
{
    public static void main(String[] args) {


        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(1);

        //map:  元素 + 1
        //再取奇数
        env.fromElements(1,2,3)
           .flatMap((Integer num,Collector<Integer> out) -> {
               num += 1;
               if (num % 2 == 1){
                   out.collect(num);
               }
           }).returns(Types.INT)
           .print();

        try {
                    env.execute();
                } catch (Exception e) {
                    e.printStackTrace();
                }

    }
}
